package com.example.test_witns.utils;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class FriendsDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "friends_db.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "friends";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_AVATAR = "avatar";
    private static final String COLUMN_LAST_MESSAGE = "last_message";
    private static final String COLUMN_LAST_CONTACT = "last_contact";

    public long addFriend(String name, byte[] avatar, String lastMessage, String lastContact) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FriendsDatabaseHelper.COLUMN_NAME, name);
        values.put(FriendsDatabaseHelper.COLUMN_AVATAR, avatar);
        values.put(FriendsDatabaseHelper.COLUMN_LAST_MESSAGE, lastMessage);
        values.put(FriendsDatabaseHelper.COLUMN_LAST_CONTACT, lastContact);
        long newRowId = db.insert(FriendsDatabaseHelper.TABLE_NAME, null, values);
        db.close();

        return newRowId;
    }

    public void loadFriends(FriendsLoadedListener listener){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
        while (cursor.moveToNext()){
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex(COLUMN_NAME));
            @SuppressLint("Range") byte[] avatar = cursor.getBlob(cursor.getColumnIndex(COLUMN_AVATAR));
            @SuppressLint("Range") String lastMessage = cursor.getString(cursor.getColumnIndex(COLUMN_LAST_MESSAGE));
            @SuppressLint("Range") String lastContact = cursor.getString(cursor.getColumnIndex(COLUMN_LAST_CONTACT));

            listener.onFriendLoaded(id, name, avatar, lastMessage, lastContact);
        }
    }
    public interface FriendsLoadedListener {
        void onFriendLoaded(int id, String name, byte[] avatar, String lastMessage, String lastContact);
    }

    private static final String Friends_Info_TABLE_CREATE =
            "CREATE TABLE " + TABLE_NAME + " (" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_NAME + " TEXT, " +
            COLUMN_AVATAR + " BLOB, " +
            COLUMN_LAST_MESSAGE + " TEXT, " +
            COLUMN_LAST_CONTACT + " TEXT" +
            ");";

    public FriendsDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(Friends_Info_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}